class Solution {
private:
    vector<vector<string> > result;
    void backtracking(vector<string> &chessboard, int n, int row)
    {
        if(row == n)
        {
            result.push_back(chessboard);
            return;
        }
        for(int col = 0; col < n; col++)
        {
            if(is_valid(chessboard, row, col, n))
            {
                chessboard[row][col] = 'Q';
                backtracking(chessboard, n, row + 1);
                chessboard[row][col] = '.';
            }
        }
        return;
    }
    bool is_valid(vector<string> &chessboard, int row, int col, int n)
    {
        for(int i = 0; i < n; i++)
        {
            if(chessboard[i][col] == 'Q')
            {
                return false;
            }
        }

        for(int i = row, j = col; i >= 0 && j >= 0; i--, j--)
        {
            if(chessboard[i][j] == 'Q')
            {
                return false;
            }
        }

        for(int i = row, j = col; i >= 0 && j >= 0; i--, j++)
        {
            if(chessboard[i][j] == 'Q')
            {
                return false;
            }
        }

        return true;
    }
public:
    vector<vector<string>> solveNQueens(int n) {
        vector<string> chessboard(n, string(n, '.'));
        backtracking(chessboard, n, 0);
        return result;
    }
};